package com.blog1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog1.common.exception.blogException;
import com.blog1.common.util.BeanTransUtils;
import com.blog1.enums.ResultCodeEnum;
import com.blog1.mapper.BlogInfoMapper;
import com.blog1.pojo.request.AddBlogInfo;
import com.blog1.pojo.request.UpdateBlogRequest;
import com.blog1.pojo.response.BlogInfoListResult;
import com.blog1.pojo.response.BlogInfoResult;
import com.blog1.service.BlogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blog1.pojo.dataobject.BlogInfo;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("blogInfoServiceImpl")
public class BlogInfoServiceImpl implements BlogInfoService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    public List<BlogInfoListResult> getList() {
        // 创建一个LambdaQueryWrapper对象，用于查询BlogInfo表中deleteFlag为0的记录
        LambdaQueryWrapper<BlogInfo> wrapper=new QueryWrapper<BlogInfo>()
                .lambda().eq(BlogInfo::getDeleteFlag,0)
                .orderByDesc(BlogInfo::getId);
       // 根据wrapper查询BlogInfo表中的记录
       List<BlogInfo>blogInfos=blogInfoMapper.selectList(wrapper);
       // 将查询结果转换为BlogInfoListResult对象列表
       List<BlogInfoListResult>blogInfoListResults=blogInfos.stream().map(blogInfo -> {
       return BeanTransUtils.trans(blogInfo);
       }).collect(Collectors.toList());
return blogInfoListResults;
    }

    @Override
    public BlogInfoResult getBlogDetail(int id) {
        LambdaQueryWrapper<BlogInfo>wrapper=new QueryWrapper<BlogInfo>()
                .lambda().eq(BlogInfo::getId,id)
                .eq(BlogInfo::getDeleteFlag,0);
BlogInfo blogInfo=blogInfoMapper.selectOne(wrapper);
        return BeanTransUtils.trans1(blogInfo);
    }

    @Override
    public boolean addBlog(AddBlogInfo addBlogInfo) {
        if(addBlogInfo==null){
            throw new blogException(ResultCodeEnum.NOT_FOUND.getCode(), "addBlogInfo为空");
        }
        try{
            BlogInfo blogInfo=new BlogInfo();
            BeanUtils.copyProperties(addBlogInfo,blogInfo);
            Integer result=blogInfoMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
        }catch (Exception e){
            log.error("博客发布失败");
        }
        return false;
        }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest,blogInfo);
        Integer result=null;
        try{
            result=blogInfoMapper.updateById(blogInfo);
      if(result>=1){
          return true;
      }
        }catch (Exception e){
            log.error("博客更新失败");
        }
        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try{
            Integer result=blogInfoMapper.updateById(blogInfo);
            if(result>=1)
                return true;
        }catch (Exception e){
            log.error("博客删除失败");
        }
        return false;
    }
}
